package com.dianping.cat.alarm.receiver.transform;

import com.dianping.cat.alarm.receiver.IEntity;
import com.dianping.cat.alarm.receiver.IVisitor;
import com.dianping.cat.alarm.receiver.entity.AlertConfig;
import com.dianping.cat.alarm.receiver.entity.Receiver;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;













public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

  public DefaultXmlBuilder() { this(false); }



/*  35 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  39 */     this.m_compact = compact;
/*  40 */     this.m_sb = sb;
/*  41 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  45 */     entity.accept(this.m_visitor);
/*  46 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  50 */     this.m_level--;

/*  52 */     indent();
/*  53 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  57 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  61 */     if (value == null) {
/*  62 */       return null;
    }

/*  65 */     String str = toString(value);
/*  66 */     int len = str.length();
/*  67 */     StringBuilder sb = new StringBuilder(len + 16);

/*  69 */     for (int i = 0; i < len; i++) {
/*  70 */       char ch = str.charAt(i);

/*  72 */       switch (ch) {
        case '<':
/*  74 */           sb.append("&lt;");
          break;
        case '>':
/*  77 */           sb.append("&gt;");
          break;
        case '&':
/*  80 */           sb.append("&amp;");
          break;
        case '"':
/*  83 */           if (!text) {
/*  84 */             sb.append("&quot;");
            break;
          }
        default:
/*  88 */           sb.append(ch);
          break;
      }

    }
/*  93 */     return sb.toString();
  }

  protected void indent() {
/*  97 */     if (!this.m_compact) {
/*  98 */       for (int i = this.m_level - 1; i >= 0; i--) {
/*  99 */         this.m_sb.append("   ");
      }
    }
  }


/* 105 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 109 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 113 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 117 */     indent();

/* 119 */     this.m_sb.append('<').append(name);

/* 121 */     int len = nameValues.length;

/* 123 */     for (int i = 0; i + 1 < len; i += 2) {
/* 124 */       Object attrName = nameValues[i];
/* 125 */       Object attrValue = nameValues[i + 1];

/* 127 */       if (attrValue != null) {
/* 128 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 132 */     if (dynamicAttributes != null) {
/* 133 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 134 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 138 */     if (text != null && closed) {
/* 139 */       this.m_sb.append('>');
/* 140 */       this.m_sb.append(escape(text, true));
/* 141 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 143 */       if (closed) {
/* 144 */         this.m_sb.append('/');
      } else {
/* 146 */         this.m_level++;
      }

/* 149 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 155 */     if (value instanceof String)
/* 156 */       return (String)value; 
/* 157 */     if (value instanceof Collection) {
/* 158 */       Collection<Object> list = (Collection)value;
/* 159 */       StringBuilder sb = new StringBuilder(32);
/* 160 */       boolean first = true;

/* 162 */       for (Object item : list) {
/* 163 */         if (first) {
/* 164 */           first = false;
        } else {
/* 166 */           sb.append(',');
        }

/* 169 */         if (item != null) {
/* 170 */           sb.append(item);
        }
      }

/* 174 */       return sb.toString();
/* 175 */     }  if (value.getClass().isArray()) {
/* 176 */       int len = Array.getLength(value);
/* 177 */       StringBuilder sb = new StringBuilder(32);
/* 178 */       boolean first = true;

/* 180 */       for (int i = 0; i < len; i++) {
/* 181 */         Object item = Array.get(value, i);

/* 183 */         if (first) {
/* 184 */           first = false;
        } else {
/* 186 */           sb.append(',');
        }

/* 189 */         if (item != null) {
/* 190 */           sb.append(item);
        }
      }

/* 194 */       return sb.toString();
    }

/* 197 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 201 */     if (text == null) {
      return;
    }

/* 205 */     indent();

/* 207 */     this.m_sb.append('<').append(name);

/* 209 */     int len = nameValues.length;

/* 211 */     for (int i = 0; i + 1 < len; i += 2) {
/* 212 */       Object attrName = nameValues[i];
/* 213 */       Object attrValue = nameValues[i + 1];

/* 215 */       if (attrValue != null) {
/* 216 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 220 */     this.m_sb.append(">");
/* 221 */     this.m_sb.append(escape(text, true));
/* 222 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 226 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 230 */     indent();

/* 232 */     this.m_sb.append('<').append(name).append(">");

/* 234 */     if (escape) {
/* 235 */       this.m_sb.append(escape(text, true));
    } else {
/* 237 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 240 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


  public void visitAlertConfig(AlertConfig alertConfig) {
/* 245 */     startTag("alert-config", null, new Object[] { "enable", alertConfig.getEnable() });

/* 247 */     if (!alertConfig.getReceivers().isEmpty()) {
/* 248 */       for (Receiver receiver : alertConfig.getReceivers().values()) {
/* 249 */         receiver.accept(this.m_visitor);
      }
    }

/* 253 */     endTag("alert-config");
  }


  public void visitReceiver(Receiver receiver) {
/* 258 */     startTag("receiver", null, new Object[] { "id", receiver.getId(), "enable", receiver.getEnable() });

/* 260 */     if (!receiver.getEmails().isEmpty()) {
/* 261 */       for (String email : receiver.getEmails()) {
/* 262 */         tagWithText("email", email, new Object[0]);
      }
    }

/* 266 */     if (!receiver.getDxs().isEmpty()) {
/* 267 */       for (String dx : receiver.getDxs()) {
/* 268 */         tagWithText("dx", dx, new Object[0]);
      }
    }

/* 272 */     if (!receiver.getPhones().isEmpty()) {
/* 273 */       for (String phone : receiver.getPhones()) {
/* 274 */         tagWithText("phone", phone, new Object[0]);
      }
    }

/* 278 */     if (!receiver.getWeixins().isEmpty()) {
/* 279 */       for (String weixin : receiver.getWeixins()) {
/* 280 */         tagWithText("weixin", weixin, new Object[0]);
      }
    }

/* 284 */     endTag("receiver");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/receiver/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */